package com.shuda.business.channel.controller;

import com.shuda.business.channel.dto.DistributionSchemeDTO;
import com.shuda.business.channel.dto.DistributionSchemeQueryDTO;
import com.shuda.business.channel.service.DistributionSchemeService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.dto.PageResult;
import com.shuda.common.result.Result;
import com.shuda.common.exception.BusinessException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分发方案管理控制器
 */
@RestController
@RequestMapping("/api/channel/distribution-scheme")
@Tag(name = "分发方案管理", description = "分发方案管理接口")
public class DistributionSchemeController {

    @Autowired
    private DistributionSchemeService distributionSchemeService;

    @GetMapping("/page")
    @Operation(summary = "获取分发方案分页列表")
    public Result<PageResult<DistributionSchemeDTO>> getDistributionSchemePage(
            @Parameter(description = "分页参数") PageDTO pageDTO,
            @Parameter(description = "查询参数") DistributionSchemeQueryDTO queryDTO) {
        try {
            PageResult<DistributionSchemeDTO> result = distributionSchemeService.getDistributionSchemePage(pageDTO, queryDTO);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取分发方案详情")
    public Result<DistributionSchemeDTO> getDistributionSchemeById(
            @Parameter(description = "方案ID") @PathVariable Long id) {
        try {
            DistributionSchemeDTO result = distributionSchemeService.getDistributionSchemeById(id);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    @Operation(summary = "创建分发方案")
    public Result<Long> createDistributionScheme(
            @Parameter(description = "分发方案信息") @RequestBody DistributionSchemeDTO dto) {
        try {
            Long id = distributionSchemeService.createDistributionScheme(dto);
            return Result.success(id);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新分发方案")
    public Result<Void> updateDistributionScheme(
            @Parameter(description = "方案ID") @PathVariable Long id,
            @Parameter(description = "分发方案信息") @RequestBody DistributionSchemeDTO dto) {
        try {
            distributionSchemeService.updateDistributionScheme(id, dto);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分发方案")
    public Result<Void> deleteDistributionScheme(
            @Parameter(description = "方案ID") @PathVariable Long id) {
        try {
            distributionSchemeService.deleteDistributionScheme(id);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除分发方案")
    public Result<Void> batchDeleteDistributionScheme(
            @Parameter(description = "方案ID列表") @RequestBody List<Long> ids) {
        try {
            distributionSchemeService.batchDeleteDistributionScheme(ids);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新分发方案状态")
    public Result<Void> updateDistributionSchemeStatus(
            @Parameter(description = "方案ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam String status) {
        try {
            distributionSchemeService.updateDistributionSchemeStatus(id, status);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/list/enabled")
    @Operation(summary = "获取启用的分发方案列表")
    public Result<List<DistributionSchemeDTO>> getEnabledDistributionSchemeList() {
        try {
            List<DistributionSchemeDTO> result = distributionSchemeService.getEnabledDistributionSchemeList();
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }
}